package com.pkk.penguin.frame.know.springmvc.handlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by peikunkun on 2018/1/8 0008.
 */
/*继承extends HandlerInterceptorAdapter和实现HandlerInterceptor都可以*/
public class AuthorizationInterceptor implements HandlerInterceptor {

  /*不拦截以下请求*/
  private static final String[] IGNORE_URL = {"/loginResult", "userLogin"};


  /**
   * @param httpServletRequest
   * @param httpServletResponse
   * @param o
   * @return
   * @Desc 处理拦截器使用，在Controller之前调用，该方法返回true才会继续执行，返回false方法直接结束，不在进行后续操作
   */
  public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o)
      throws Exception {
    return false;
  }

  /**
   * @param httpServletRequest
   * @param httpServletResponse
   * @param o
   * @param modelAndView
   * @Desc 该方法只有在Controller的方法调用之后执行，可以对方法返回的ModelAndView进行操作，该方法也只能在当前的Interceptor的preHandle返回true时才会执行
   */
  public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
      ModelAndView modelAndView) throws Exception {
    System.out.println("postHandle  开始执行了");
  }

  /**
   * @param httpServletRequest
   * @param httpServletResponse
   * @param o
   * @param e
   * @Desc 该方法在整个请求之后完成执行，主要作用是清理资源 该方法只有会在preHandle方法返回true时才会执行
   */
  public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
      Exception e) throws Exception {
    System.out.println("afterCompletion  开始执行了");
  }
}
